

const type_enum =  {
  'phone': 1,
  'email': 2,
  'potato': 3,
}
import Input from '~/components/Input';

export default {
  async asyncData(context) {
    const { store,route,query, params} = context;

    let form = store.getters['register/getFormData'];
    if(params.country){
      store.commit('register/SET_AREA_CODE',params.country.code)
    } else {
      store.dispatch('register/setAreaCode')
    }
    form.user_type && delete form.user_type
    return form;
  },
  data(){
    return {
      // 验证码类型
      ver_type: {
        [type_enum.phone]: {
          loading:false,
          ver_handel:false, // 定时器
          is_ver_valid: false, //验证码是否有效
          ver_time: 60, // 验证码重试倒计时
          has_ver:false, //是否已经获取验证码
        },
        [type_enum.email]: {
          loading:false,
          ver_handel:false, // 定时器
          is_ver_valid: false, //验证码是否有效
          ver_time: 60, // 验证码重试倒计时
          has_ver:false, //是否已经获取验证码
        },
        [type_enum.potato]: {
          loading:false,
          ver_handel:false, // 定时器
          is_ver_valid: false, //验证码是否有效
          ver_time: 60, // 验证码重试倒计时
          has_ver:false, //是否已经获取验证码
        },
      },
      posting:false,
      loading:false,
      type_enum,
      origin: this.$route.query.origin,
      originType: this.$route.query.originType,
    }
  },
  computed: {
    valid_ver(){
      return (value)=>!!String(value).trim() && !this.ver_type[this.by].is_ver_valid;
    },
    valid_form(){
      let flag = true;
      if(this.by == this.type_enum.phone){
        flag = !!String(this.phone).trim();

      }else if(this.by == this.type_enum.email){
        flag = !!String(this.email).trim();
      } else {
        flag = !!String(this.potato).trim();
      }

      //是否已获取验证码
      if(!this.ver_type[this.by].has_ver){
        flag = false;
      }
      flag = flag && !!String(this.ver_code).trim();
      return flag && !this.posting;
    },
    ver_time_text(){
      return (type)=>{
        return !this.ver_type[type].is_ver_valid ? (this.ver_type[type].loading ? '加载中' : '获取验证码') : `${this.ver_type[type].ver_time}s后重试`;
      }
    },
    type_name () {
      let params = this.$route.params;
      let query = this.$route.query;
      let name = (params && params.type) ? params.type : ((query && query.type) ? query.type : 'user')
      return name
    },
  },
  components:{
    Input,
  },
  mounted(){
    this.$emit('changeBg','#fff');
  },
  methods: {
    changeRegisterUser(type){
      // 修改注册类型是个人还是机构
      this.$router.push({name: `register-${type}`,query: {type: type, origin: this.origin, originType: this.originType}})
    },
    changeAreaCode(){ // 修改手机区号
      // 缓存表单数据
      let {
          email,
          phone,
          phone_area_code,
          potato,
          potato_area_code,
          by,
        } = this.$data;

      this.$store.commit('register/SET_FORM',{
        email,
        phone,
        phone_area_code,
        potato,
        potato_area_code,
        by,
      });
      this.$router.push({name:'area_list',query:{from:'register',type:this.type_name, origin: this.origin, originType: this.originType},params:{from:this.$route.name}})
    },
    async getVerCode(e) { // 获取验证码
       let verType = this.ver_type[this.by];
      if(verType.is_ver_valid) return false;
      if(e.currentTarget.getAttribute('disabled') || verType.loading) return false;
      verType.loading = true;

      //获取验证码成功后input要获取焦点
      //这里不能反正ajax之后执行，浏览器处理机制问题
      try{
        let $input = e.target.parentNode.querySelector('input');
        $input && $input.focus();
      }catch(e){console.error(e)}

      let { data } = await this.$axios.post('?ct=member&ac=send_ver_code',{
        account:this.by == this.type_enum.phone
                ?  `${this.phone_area_code}${this.phone}`
                : ( this.by == this.type_enum.potato
                  ? `${this.potato_area_code}${this.potato}`
                  : this.email),
        action:'reg',
        type:this.by,
      });

      if(data.code == 0 ){

        verType.has_ver = true;
        verType.is_ver_valid = true;
        verType.ver_handel = setInterval(()=>{
          try {
            if(verType.ver_time == 0) {
              clearInterval(verType.ver_handel);
              verType.ver_handel = false;
              verType.ver_time = 60;
              verType.is_ver_valid = false;

            }
            verType.ver_time--;
          }catch(e){
            clearInterval(verType.ver_handel);
            verType.ver_handel = false;
            verType.is_ver_valid = false;
            this.$error(e);
          }

        },1000);

      }else{
        this.$toast(data.msg);
      }
      verType.loading = false;
    },
    changeRegisterType(by) {// 修改注册类型
      this.ver_code = '';
      this.by = by;
    },
  }
}
